"""二进制求和"""


class Solution:
    def addBinary(self, a, b):
        if len(a) < len(b):
            a, b = b, a
        n = len(a)
        b = '0' * (n - len(b)) + b  # 补齐 b 不足的位为 0
        result = ''
        summ = 0  # 进位值
        for i in range(n):
            a_1 = int(a[-i - 1])
            b_1 = int(b[-i - 1])
            result = str((a_1 + b_1 + summ) % 2) + result  # 当前位数相加模 2 ，链接更小位数的值
            summ = (a_1 + b_1 + summ) // 2  # 当前位数之和整除二，得到下一位运算的进位值

        if summ == 1:  # 判断最高位是否需要进位
            result = '1' + result
        print(result)
        return result


if __name__ == '__main__':
    a = "1010"
    b = "1011"
    s = Solution()
    s.addBinary(a, b)
